package com.example.child_protection.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.child_protection.dao.ChildArchivesDao;
import com.example.child_protection.dao.SystemFeedingProgramDao;
import com.example.child_protection.dto.info.SystemFeedingProgramDTO;
import com.example.child_protection.dto.request.SimpleIdRequestDTO;
import com.example.child_protection.dto.request.find.SystemFeedingProgramRequestDTO;
import com.example.child_protection.dto.response.SystemFeedingProgramListResponseDTO;
import com.example.child_protection.dto.response.SystemFeedingProgramResponseDTO;
import com.example.child_protection.enums.ServiceActionEnums;
import com.example.child_protection.model.ChildArchivesModel;
import com.example.child_protection.model.SystemFeedingProgramModel;
import com.example.child_protection.service.SystemFeedingProgramQueryService;
import com.example.child_protection.service.base.MyBaseService;
import com.example.child_protection.service.base.MyServiceExecutor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * 喂养方案查询service实现层
 *
 * @author Biz
 * @date 2018-11-2
 */
@Component
public class SystemFeedingProgramQueryServiceImpl extends MyBaseService implements SystemFeedingProgramQueryService {

    @Autowired
    public SystemFeedingProgramDao systemFeedingProgramDao;

    @Autowired
    public ChildArchivesDao childArchivesDao;

    /**
     * id查询
     *
     * @param requestDTO
     * @return
     */
    public SystemFeedingProgramResponseDTO findId(SimpleIdRequestDTO requestDTO) {
        return this.service(ServiceActionEnums.SYSTEM_FEEDING_PROGRAM_FIND_ID, requestDTO, SystemFeedingProgramResponseDTO.class, new MyServiceExecutor<SimpleIdRequestDTO, SystemFeedingProgramResponseDTO>() {
            @Override
            protected SystemFeedingProgramResponseDTO myprocess(SimpleIdRequestDTO requestDTO) throws Exception {
                SystemFeedingProgramResponseDTO responseDTO = new SystemFeedingProgramResponseDTO();
                SystemFeedingProgramModel systemFeedingProgramModel = systemFeedingProgramDao.findId(requestDTO.getId());
                SystemFeedingProgramDTO systemFeedingProgram = new SystemFeedingProgramDTO();
                BeanUtils.copyProperties(systemFeedingProgramModel, systemFeedingProgram);
                convertValues(systemFeedingProgram);
                responseDTO.setInfo(systemFeedingProgram);
                responseDTO.successfully();
                return responseDTO;
            }
        });
    }

    /**
     * 根据档案ID查询
     *
     * @param requestDTO
     * @return
     */
    public SystemFeedingProgramListResponseDTO findByArchivesId(SimpleIdRequestDTO requestDTO) {
        return this.service(ServiceActionEnums.SYSTEM_FEEDING_PROGRAM_FIND_LIST, requestDTO, SystemFeedingProgramListResponseDTO.class, new MyServiceExecutor<SimpleIdRequestDTO, SystemFeedingProgramListResponseDTO>() {
            @Override
            protected SystemFeedingProgramListResponseDTO myprocess(SimpleIdRequestDTO requestDTO) throws Exception {
                SystemFeedingProgramListResponseDTO responseDTO = new SystemFeedingProgramListResponseDTO();
                SystemFeedingProgramModel req = new SystemFeedingProgramModel();
                ChildArchivesModel childArchivesModel = childArchivesDao.findId(requestDTO.getId());

                Long timer = childArchivesModel.getAge().getTime();
                Long timer1 = new Date().getTime();

                Long s = timer1 - timer;
                Long daysCount = s/1000/60/60/24;
                Long month = daysCount/30;
                Long days = daysCount%30;
                req.setMonth(Integer.parseInt(month+""));
                req.setDays(Integer.parseInt(days+""));

                List<SystemFeedingProgramModel> lists = systemFeedingProgramDao.findPage(req);
                List<SystemFeedingProgramDTO> systemFeedingPrograms = JSONObject.parseArray(JSONArray.toJSONString(lists), SystemFeedingProgramDTO.class);
                for (SystemFeedingProgramDTO systemFeedingProgram : systemFeedingPrograms) {
                    convertValues(systemFeedingProgram);
                }

                responseDTO.setInfos(systemFeedingPrograms);
                responseDTO.successfully();
                return responseDTO;
            }
        });
    }

    /**
     * 列表查询
     *
     * @param requestDTO
     * @return
     */
    public SystemFeedingProgramListResponseDTO findList(SystemFeedingProgramRequestDTO requestDTO) {
        return this.service(ServiceActionEnums.SYSTEM_FEEDING_PROGRAM_FIND_LIST, requestDTO, SystemFeedingProgramListResponseDTO.class, new MyServiceExecutor<SystemFeedingProgramRequestDTO, SystemFeedingProgramListResponseDTO>() {
            @Override
            protected SystemFeedingProgramListResponseDTO myprocess(SystemFeedingProgramRequestDTO requestDTO) throws Exception {
                SystemFeedingProgramListResponseDTO responseDTO = new SystemFeedingProgramListResponseDTO();
                SystemFeedingProgramModel req = new SystemFeedingProgramModel();
                BeanUtils.copyProperties(requestDTO, req);
                List<SystemFeedingProgramModel> lists = systemFeedingProgramDao.findPage(req);
                List<SystemFeedingProgramDTO> systemFeedingPrograms = JSONObject.parseArray(JSONArray.toJSONString(lists), SystemFeedingProgramDTO.class);
                for (SystemFeedingProgramDTO systemFeedingProgram : systemFeedingPrograms) {

                    convertValues(systemFeedingProgram);
                }
                responseDTO.setInfos(systemFeedingPrograms);
                responseDTO.successfully();
                return responseDTO;
            }
        });
    }

    /**
     * 转换方案月龄显示值
     */
    public void convertValues(SystemFeedingProgramDTO dto) {
        StringBuffer value = new StringBuffer("");
        value.append("（");
        if (null != dto.getBeginMonth() && 0 != dto.getBeginMonth()) {
            value.append(dto.getBeginMonth() + "月");
        }
        if (null != dto.getBeginDays() && 0 != dto.getBeginDays()) {
            value.append(dto.getBeginDays() + "天");
        }
        value.append("～");
        if (null != dto.getEndMonth() && 0 != dto.getEndMonth()) {
            value.append(dto.getEndMonth() + "月");
        }
        if (null != dto.getEndDays() && 0 != dto.getEndDays()) {
            value.append(dto.getEndDays() + "天");
        } else {
            value.append("龄");
        }

        value.append("）");
        dto.setValues(value.toString());
    }
//    public static void main (String[] args){
//        System.out.println(89%30);
//    }
}
